package cn.trs.common.utils;

import cn.trs.common.enums.ErrorCodeEnum;
import cn.trs.common.enums.SuccCodeEnum;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;

import java.io.Serializable;

/**
 * @version 1.0
 * @author:TRS
 * @create:2023/2/23 9:22
 * @description:
 * @ClassName: ResponseEntityUtil
 * @company TRS
 */
public class ResponseEntityUtil<T> implements Serializable {

    private static final long serialVersionUID = -8547608350406150202L;
    private boolean success;
    private String message;
    private String method;
    private T object;
    private String code;
    private String requestId = TraceContext.traceId();

    public ResponseEntityUtil(){

    }

    public ResponseEntityUtil(boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    public ResponseEntityUtil(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseEntityUtil(String method) {
        this.success = true;
        this.code = SuccCodeEnum.DATA_SUCC.getCode();
        this.message = SuccCodeEnum.DATA_SUCC.getMessage();
        this.method = method;
    }

    public ResponseEntityUtil(T object) {
        this.success = true;
        this.code = SuccCodeEnum.DATA_SUCC.getCode();
        this.message = SuccCodeEnum.DATA_SUCC.getMessage();
        this.object = object;
    }

    public ResponseEntityUtil(String method, T object) {
        this.success = true;
        this.code = SuccCodeEnum.DATA_SUCC.getCode();
        this.message = SuccCodeEnum.DATA_SUCC.getMessage();
        this.method = method;
        this.object = object;
    }

    public ResponseEntityUtil(boolean success, String code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public ResponseEntityUtil(boolean success, String message, T object) {
        this.success = success;
        this.message = message;
        this.object = object;
    }

    public ResponseEntityUtil(boolean success, String message, String method, T object) {
        this.success = success;
        this.message = message;
        this.method = method;
        this.object = object;
    }

    public ResponseEntityUtil(boolean success, String code, String message, String method, T object) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.method = method;
        this.object = object;
    }

    public static <T> ResponseEntityUtil<T> ok(){
        ResponseEntityUtil<T> responseEntityUtil = new ResponseEntityUtil<>();
        responseEntityUtil.code = SuccCodeEnum.DATA_SUCC.getCode();
        responseEntityUtil.message = SuccCodeEnum.DATA_SUCC.getMessage();
        responseEntityUtil.success = true;
        responseEntityUtil.requestId = TraceContext.traceId();

        return responseEntityUtil;
    }

    public static <T> ResponseEntityUtil<T> ok(T object){
        ResponseEntityUtil<T> responseEntityUtil = new ResponseEntityUtil<>();
        responseEntityUtil.code = SuccCodeEnum.DATA_SUCC.getCode();
        responseEntityUtil.message = SuccCodeEnum.DATA_SUCC.getMessage();
        responseEntityUtil.success = true;
        responseEntityUtil.object = object;
        responseEntityUtil.requestId = TraceContext.traceId();

        return responseEntityUtil;
    }

    public static <T> ResponseEntityUtil<T> ok(String message, T object){
        ResponseEntityUtil<T> responseEntityUtil = new ResponseEntityUtil<>();
        responseEntityUtil.code = SuccCodeEnum.DATA_SUCC.getCode();
        responseEntityUtil.message = message;
        responseEntityUtil.success = true;
        responseEntityUtil.object = object;
        responseEntityUtil.requestId = TraceContext.traceId();

        return responseEntityUtil;
    }

    public static <T> ResponseEntityUtil<T> error(String failMsg){
        ResponseEntityUtil<T> responseEntityUtil = new ResponseEntityUtil<>();
        responseEntityUtil.code = ErrorCodeEnum.COMMON_ERROR.getCode();
        responseEntityUtil.message = failMsg;
        responseEntityUtil.success = false;
        responseEntityUtil.requestId = TraceContext.traceId();

        return responseEntityUtil;
    }

    public static <T> ResponseEntityUtil<T> error(String code, String message){
        ResponseEntityUtil<T> responseEntityUtil = new ResponseEntityUtil<>();
        responseEntityUtil.code = code;
        responseEntityUtil.message = message;
        responseEntityUtil.success = false;
        responseEntityUtil.requestId = TraceContext.traceId();

        return responseEntityUtil;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
}
